package com.rgs.base.tasks.excel;

import com.google.common.base.Preconditions;
import com.rgs.base.core.dto.CommonDateListPageRequestDTO;
import com.rgs.base.core.dto.CommonListPageRequestDTO;
import com.rgs.base.core.function.CheckFunction;
import com.rgs.base.core.model.WebApiReturnResultModel;
import com.rgs.base.core.page.IPageRequestModel;
import com.rgs.base.core.page.IPageResponseModel;
import com.rgs.base.core.system.AsyncTaskTypeEnum;
import com.rgs.base.core.util.excel.bo.ExcelConfigBO;
import com.rgs.base.core.util.excel.bo.ExcelDynamicHeadBO;
import com.rgs.base.tasks.task.AsyncTaskRunnableBO;
import com.rgs.base.tasks.task.AsyncTaskThreadPool;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

/**
 * @author Ren Gaoshuai
 * @date 2022-03-08 15:45
 **/
public class ExportExcelBuildFactory<T> {
    AsyncTaskTypeEnum asyncTaskTypeEnum;
    IPageRequestModel pageRequestModel;
    String organizeId;
    Class<T> tClass;
    CheckFunction<IPageRequestModel, IPageResponseModel> checkFunction;
    ExcelConfigBO excelConfig;
    List<T> totalList;

    public static <T> ExportExcelBuildFactory<T> build(Class<T> tClass, AsyncTaskTypeEnum asyncTaskTypeEnum, String exportTitle) {
        ExportExcelBuildFactory<T> exportExcelBuildFactory = new ExportExcelBuildFactory<>();
        exportExcelBuildFactory.tClass = tClass;
        exportExcelBuildFactory.asyncTaskTypeEnum = asyncTaskTypeEnum;
        exportExcelBuildFactory.excelConfig = new ExcelConfigBO(exportTitle);
        return exportExcelBuildFactory;
    }

    public static <T> ExportExcelBuildFactory<T> build(Class<T> tClass, AsyncTaskTypeEnum asyncTaskTypeEnum) {
        return ExportExcelBuildFactory.build(tClass, asyncTaskTypeEnum, asyncTaskTypeEnum.getTaskName());
    }

    public ExportExcelBuildFactory<T> titleMap(Map<String, String> titleMap) {
        this.excelConfig.setTitleMap(titleMap);
        return this;
    }

    public ExportExcelBuildFactory<T> dynamicHeadMap(Map<String, ExcelDynamicHeadBO> dynamicHeadMap) {
        this.excelConfig.setDynamicHeadMap(dynamicHeadMap);
        return this;
    }

    public ExportExcelBuildFactory<T> ignoreSet(Set<String> ignoreSet) {
        this.excelConfig.setIgnoreSet(ignoreSet);
        return this;
    }

    /**
     * 2022/1/6
     * 小区模块导出使用 带日期
     */
    public ExportExcelBuildFactory<T> commonDateListPageRequest(CommonDateListPageRequestDTO commonDateListPageRequest) {
        this.pageRequestModel = commonDateListPageRequest;
        return this;
    }

    public ExportExcelBuildFactory<T> commonListPageRequest(CommonListPageRequestDTO commonListPageRequest) {
        this.pageRequestModel = commonListPageRequest;
        //this.organizeId = commonChildOrganizeListPageRequest.getChildOrganizeId();
        return this;
    }

    public ExportExcelBuildFactory<T> organizeId(String organizeId) {
        this.organizeId = organizeId;
        return this;
    }


//    public ExportExcelBuildFactory<T> commonChildOrganizeListPageRequest(CommonRecursionChildOrgPageRequestDTO commonChildOrganizeListPageRequest) {
//        this.pageRequestModel = commonChildOrganizeListPageRequest;
//        this.organizeId = commonChildOrganizeListPageRequest.getChildOrganizeId();
//        return this;
//    }
//
//    public ExportExcelBuildFactory<T> commonListPageRequest(CommonOrganizeIdPageRequestDTO commonListPageRequest) {
//        this.pageRequestModel = commonListPageRequest;
//        this.organizeId = commonListPageRequest.getOrganizeId();
//        return this;
//    }

    public ExportExcelBuildFactory<T> checkFunction(CheckFunction<IPageRequestModel, IPageResponseModel> checkFunction) {
        this.checkFunction = checkFunction;
        return this;
    }

    public ExportExcelBuildFactory<T> excelConfig(ExcelConfigBO excelConfig) {
        this.excelConfig = excelConfig;
        return this;
    }

    public ExportExcelBuildFactory<T> totalList(List<T> totalList) {
        this.totalList = totalList;
        return this;
    }

    public ExportExcelBuildFactory<T> total(T totalList) {
        this.totalList = Collections.singletonList(totalList);
        return this;
    }

    public Future<WebApiReturnResultModel<Void>> export() {
        Preconditions.checkNotNull(this.organizeId);
        Preconditions.checkNotNull(this.asyncTaskTypeEnum);
        Preconditions.checkNotNull(this.pageRequestModel);
        Preconditions.checkNotNull(this.tClass);
        Preconditions.checkNotNull(this.checkFunction);
        Preconditions.checkNotNull(this.excelConfig);

        AsyncTaskRunnableBO asyncTaskRunnable = new AsyncTaskRunnableBO(
                this.organizeId,
                this.asyncTaskTypeEnum,
                null);

        DefaultExportExcelHandle<T> defaultExportExcelHandle
                = new DefaultExportExcelHandle<>(this.tClass, this.pageRequestModel,
                this.checkFunction, excelConfig);

        defaultExportExcelHandle.setTotal(this.totalList);
        return AsyncTaskThreadPool.executeTask(asyncTaskRunnable, defaultExportExcelHandle);
    }

}
